$(function () {
	vm.getColModelList();
});

var vm = new Vue({
	el: '#rrapp',
	data: {
        showList: true,
        title: null,
		baseProductDiscount: {},
		productDiscount: {},
		ruleValidate: {
			name: [
				{required: true, message: '名称不能为空', trigger: 'blur'}
			]
		},
		q: {
			carSerie: '',carModel:'',code:''
		}
	},
	methods: {
		getColModelList:function(){
			Ajax.request({
                url: "../base/discount/queryAll",
                async: true,
                successCallback: function (r) {
                	var colModelList=[{label: 'id', name: 'id', index: 'id', key: true, hidden: true},
    	                  {label: 'product_id', name: 'product_id', index: 'product_id', hidden: true},
    	                  {label: '车型', name: 'carModel.carSerie', index: 'product_id', width: 80},
    	                  {label: '车系', name: 'carModel.carModel', index: 'product_id', width: 80},
    	                  {label: '代码', name: 'carModel.code', index: 'product_id', width: 80},
    	                  {label: '配置', name: 'carModel.selection', index: 'product_id', width: 80}];
                	if(r.list&&r.list.length>0){
                		for (var i = 0; i < r.list.length; i++) {
                			colModelList.push({label:r.list[i].name,name:r.list[i].mapName,index:r.list[i].mapName,width:80});
						}
                	}
                	$("#jqGrid").Grid({
                        url: '../base/productdiscount/list',
                        colModel: colModelList
                    });
                }
            });
		},
		query: function () {
			vm.reload();
		},
		add: function () {
			vm.showList = false;
			vm.title = "新增";
			vm.baseProductDiscount = {};
		},
		update: function (event) {
            var id = getSelectedRow("#jqGrid");
			if (id == null) {
				return;
			}
			
			vm.showList = false;
            vm.title = "修改";
            var dataRow=getSelectedRowData('#jqGrid');
            vm.productDiscount.productId=dataRow.product_id;
            vm.getInfo(dataRow.product_id);
		},
		saveDiscount: function (event) {
			var url = "../base/productdiscount/save";
			Ajax.request({
				url: url,
				params: JSON.stringify(vm.productDiscount),
				type: "POST",
				contentType: "application/json",
				successCallback: function (r) {
					alert('操作成功', function (index) {
						vm.reload();
					});
				}
			});
		},
		saveOrUpdate: function (event) {
            var url = vm.baseProductDiscount.id == null ? "../baseproductdiscount/save" : "../baseproductdiscount/update";
            Ajax.request({
			    url: url,
                params: JSON.stringify(vm.baseProductDiscount),
                type: "POST",
			    contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
			});
		},
		del: function (event) {
            var ids = getSelectedRows("#jqGrid");
			if (ids == null){
				return;
			}

			confirm('确定要删除选中的记录？', function () {
                Ajax.request({
				    url: "../baseproductdiscount/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
				    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
					}
				});
			});
		},
		getInfo: function(id){
			Ajax.request({
				url: "../base/productdiscount/info/"+id,
				async: true,
				successCallback: function (r) {
					if(r.productDiscount){
						vm.productDiscount = r.productDiscount;
					}
				}
			});
            /*Ajax.request({
                url: "../baseproductdiscount/info/"+id,
                async: true,
                successCallback: function (r) {
                    vm.baseProductDiscount = r.baseProductDiscount;
                }
            });*/
		},
		reload: function (event) {
			vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
			$("#jqGrid").jqGrid('setGridParam', {
                postData: {'carSerie': vm.q.carSerie,'carModel': vm.q.carModel,'code': vm.q.code},
                page: 1
            }).trigger("reloadGrid");
//            vm.handleReset('formValidate');
		},
        reloadSearch: function() {
            vm.q = {
        		carSerie: '',carModel:'',code:''
            };
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleDiscountSubmit: function (name) {
        	handleSubmitValidate(this, name, function () {
        		vm.saveDiscount()
        	});
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        }
	}
});